<?php
	
require '../include/user_login.inc.php';
require '../include/paging.inc.php';

	
	
if($_SESSION['ACCESS_SID'] != 2) redirect(HTML_BASE);

$redirect = $_SERVER['HTTP_REFERER'];	
	$user_id = $_SESSION['USER_SID'];

if ($_POST['is_submitted']) {
		$error_message = '';
		
		if (!$_POST['title']) $error_message .= 'Не е въведенa Тема<br/>';
		if (!$_SESSION['client']) $error_message .= 'Не е избран клиент<br/>';
		
		if (!$error_message) {
			$sql = "INSERT INTO tasks
						 (title,priority,status,by_user)
					   VALUES
						 ('".escape($_POST['title'])."','".$_POST['priority']."','1','".$_SESSION['client']."')
					   
					";

				query($sql);
				$id = mysql_insert_id();
				redirect(HTML_BASE.$_SESSION["lang"]."/correspondence/".$id);
				
			
		}
	}
	







$lang = 1;
$sql = "SELECT
			t.task_id,
			t.title,
			t.priority,
			t.status,
			t.by_user,
			t.creation_time
		FROM
			tasks AS t,
			law_users AS l
		WHERE
			l.law_id = '$user_id'
			AND
			l.use_id = t.by_user
		
			
		";


if (!isset($_GET['sort'])) $_GET['sort'] = 1;
if (!isset($_GET['sortBy'])) $_GET['sortBy'] = 6;

$paging = new Paging($sql, $_GET['sortBy'], $_GET['sort'], array(6=>"Подредба"), 30);
$result = $paging->query();
	
	head('Dashbord');
	if($_SESSION['ACCESS_SID'] == 2) menu_law();

?>



			<?php if ($_SESSION['message']) { ?>
            <div class="message green">
                <?=$_SESSION['message']?>
            </div>
            <?php unset($_SESSION['message']); } ?>
            
            <?php if ($error_message) { ?>
            <div class="message red">
                <?=$error_message?>
            </div>
            <?php } ?>
                
        <div id="newtask">	
		
		<form action="<?=HTML_BASE.$_SESSION["lang"]."/law/"?>" method="post" enctype="multipart/form-data">
		<input type="hidden" name="is_submitted" value="1" />
		
		<h4>Нова кореспонденция
        
        <?php 
				if($_SESSION['client'])
				{
					echo ' с '.get_user_name($_SESSION['client']);
				}else{
		?>
        
        		(избери клиент)
        <?php
					
				}
		?>
        
        </h4><br> 
		
		<label>Тема/Заглавие
		<input class="inputbox" name="title" type="text" ></label>
		
		
		<input type="submit" name="submit" id="submit" class="button" value="Старт">
		
		<br>
		
		
		<label>
		<input type="checkbox" name="priority" value="1"> Отбележи като спешно</label>
				
		</form>
		
		</div>
			
		<div id="content">

            <h3>Последни кореспонденции</h3>
            
        <table id="listing" width="99%" cellpadding="0" cellspacing="0">
		<thead>
			<tr>
			<td>Тема</td>
			<td>Клиент</td>
			<td>Дата</td>
			</tr>
		</thead>
		<tbody>
		<?php
		  $i=1;
		  while ($row = mysql_fetch_object($result)) {				  
		?>
				<tr class="<?=(($i%2)?"none":"zebra")?>">
					
					<td style="border-left:4px solid <?=($row->priority == 1)?'#F30"':'#666'?> ">					
					
						<a href="<?=HTML_BASE.$_SESSION["lang"]?>/correspondence/<?=$row->task_id?>" title="Преглед"><?=sth($row->title)?></a>
					</td>
					
					<td>
						<?=get_user_name($row->by_user)?>
					</td>
					
					<td class="switch">
						<?=date('d M Y', strtotime($row->creation_time))?>	
					</td>
					
					
				</tr>
         <?php
              $i++;    
            } 
         ?>				  
			</tbody>
		</table>
        
        <div class="pagingwrap"><div class="paging">
			<?=$paging->display()?>
		</div></div>
			
			
   </div>
    

<?php
foot();
?>